package _0util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhen
 * @version 1.0
 * @date 2020/4/3 19:20
 */
public class CmdUtil {


    /**
     * Runtime.getRuntime().exec(cmd) 不能执行path环境变量中的指令，因为是单独开启一个进程运行系统指令，无法执行到path
     * @param cmd
     * @return
     */
    public static List<String> exec(String cmd) {
        Process process = null;
        List<String> result = new ArrayList<String>();
        BufferedReader bReader = null;
        try {
            process = Runtime.getRuntime().exec(cmd);

            bReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
            String line = null;
            while ((line = bReader.readLine()) != null) {
                result.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            try {
                bReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            process.destroy();
        }

        return result;
    }


    public static List<String> exec(String cmd, String filePath) {
        Process process = null;
        BufferedReader bReader=null;
        List<String> result = new ArrayList<String>();
        try {
            if (filePath != null) {
                process = Runtime.getRuntime().exec(cmd, null, new File(filePath));
            } else {
                process = Runtime.getRuntime().exec(cmd);
            }
            bReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
            String line = null;
            while ((line = bReader.readLine()) != null) {
                result.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            process.destroy();
        }
        return result;
    }



}
